package com.ruoyi.loss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.loss.domain.bo.WellWorkRecordBo;
import com.ruoyi.loss.domain.vo.WellWorkRecordVo;
import com.ruoyi.loss.domain.WellWorkRecord;
import com.ruoyi.loss.mapper.WellWorkRecordMapper;
import com.ruoyi.loss.service.IWellWorkRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 漏井作业记录Service业务层处理
 *
 * @author toteny
 * @date 2024-01-02
 */
@RequiredArgsConstructor
@Service
public class WellWorkRecordServiceImpl implements IWellWorkRecordService {

    private final WellWorkRecordMapper baseMapper;

    /**
     * 查询漏井作业记录
     */
    @Override
    public WellWorkRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询漏井作业记录列表
     */
    @Override
    public TableDataInfo<WellWorkRecordVo> queryPageList(WellWorkRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WellWorkRecord> lqw = buildQueryWrapper(bo);
        Page<WellWorkRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询漏井作业记录列表
     */
    @Override
    public List<WellWorkRecordVo> queryList(WellWorkRecordBo bo) {
        LambdaQueryWrapper<WellWorkRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WellWorkRecord> buildQueryWrapper(WellWorkRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WellWorkRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWellId()), WellWorkRecord::getWellId, bo.getWellId());
        lqw.eq(StringUtils.isNotBlank(bo.getWellNo()), WellWorkRecord::getWellNo, bo.getWellNo());
        lqw.eq(StringUtils.isNotBlank(bo.getWellLayer()), WellWorkRecord::getWellLayer, bo.getWellLayer());
        lqw.eq(StringUtils.isNotBlank(bo.getWellHorizon()), WellWorkRecord::getWellHorizon, bo.getWellHorizon());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateDept()), WellWorkRecord::getCreateDept, bo.getCreateDept());
        return lqw;
    }

    /**
     * 新增漏井作业记录
     */
    @Override
    public Boolean insertByBo(WellWorkRecordBo bo) {
        WellWorkRecord add = BeanUtil.toBean(bo, WellWorkRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改漏井作业记录
     */
    @Override
    public Boolean updateByBo(WellWorkRecordBo bo) {
        WellWorkRecord update = BeanUtil.toBean(bo, WellWorkRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WellWorkRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除漏井作业记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
